from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import time

from sc.ZDao import getConnect

# 指定 chromedriver 路径
driver_path = r"D:\desktop\chromedriver.exe"  # 替换为你的实际路径

# 创建 Chrome 选项
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_argument(
    "user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.6998.179 Safari/537.36")

# 启动浏览器
service = Service(executable_path=driver_path)
driver = webdriver.Chrome(service=service, options=options)

try:
    # 01L00O80EO062
    # 访问智联招聘
    # driver.get("https://www.zhaopin.com")
    # driver.get("https://www.zhaopin.com/sou/jl694/kw01L00O80EO062")
    driver.get("https://www.zhaopin.com/sou/jl530/kw01L00O80EO062/p2")

    # 等待页面加载（可根据需要调整时间）
    time.sleep(3)

    # 获取页面源码并用BeautifulSoup解析
    page_source = driver.page_source
    soup = BeautifulSoup(page_source, 'html.parser')

    # 示例：提取职位信息（根据实际页面结构调整选择器）
    job_listings = soup.find_all('div', class_='joblist-box__item')  # 这只是示例class名
    # 创建一个list存放job相关信息的字典
    job_list = []
    for job in job_listings:
        job_dic = {}
        title = job.find('a', class_='jobinfo__name').get_text(strip=True)
        company = job.find('a', class_='companyinfo__name').get_text(strip=True)
        # 岗位名称
        job_name = job.select('.jobinfo__name')[0].get_text()
        # print(company_name)
        # 工资
        job_salary = job.select('.jobinfo__salary')[0].get_text()
        job_salary = job_salary.strip().replace(' ', '').replace('\n', '')
        str2 = job_salary  # 1万-2万，取的是1万

        if '天' in str2:
            str2 = int(str2.split("-")[0]) * 30
        elif ('千' in str2):
            str2 = (int(str2.split('千')[0]) * 1000)

        elif ('元' in str2):
            str2 = (int((str2.split('元'))[0].split('-')[0]))
        elif ('万' in str2):  # 含万
            str2 = float(str2.split("万")[0].split('-')[0]) * 10000
        else:
            str2 = 5000
        print(str2)
        # 获取工作地点、经验要求、学历要求
        lis = job.select('.jobinfo__other-info-item')
        job_loc = lis[0].get_text()
        job_exp = lis[1].get_text()
        job_edu = lis[2].get_text()
        job_dic['job_name'] = job_name
        job_dic['company_name'] = company
        job_dic['job_salary'] = str2
        job_loc = job_loc.split("-")[0]
        job_dic['job_loc'] = job_loc
        job_dic['job_exp'] = job_exp
        job_dic['job_edu'] = job_edu
        job_list.append(job_dic)
        print(f"职位: {title} | 公司: {company}")

    # 或者使用Selenium直接查找元素
    # jobs = driver.find_elements(By.CLASS_NAME, 'joblist-box__item')
    # for job in jobs:
    #     title = job.find_element(By.CLASS_NAME, 'jobinfo__name').text
    #     print(title)
    # 操作数据库,将数据存储进去
    db = getConnect();
    # 使用cursor()方法获取操作游标,即操作数据库的一个对象
    cursor = db.cursor()
    # 使用格式化输出的方式将变量传进去
    for i in job_list:
        sql = "INSERT INTO jobs(jid,job_name,company_name,job_sal,job_loc,job_exp,job_edu) VALUES(null,'%s','%s','%s','%s','%s','%s')" % (
            i["job_name"], i["company_name"], i["job_salary"], i["job_loc"], i["job_exp"], i["job_edu"])
        # 执行多条插入语句使用cursor.executemany(sql,list)
        # 这个list是由每条数据组成的元组
        # 执行语句
        try:
            cursor.execute(sql)
            # 更新操作需要提交
            db.commit()
            print('插入数据成功!')
        except Exception as e:
            print(e)
            print('插入数据失败!')
            # 如果有异常需要回滚
            db.rollback()
    # 关闭游标
    cursor.close()
    # 关闭数据库连接
    db.close()
    print("爬取数据成功")
finally:
    # 关闭浏览器
    driver.quit()